import { Component, OnInit, ViewChild } from '@angular/core';
import { SelectionModel } from '@angular/cdk/collections';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { MatDialog } from '@angular/material';
import { Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import { tap } from 'rxjs/operators';
import { User } from '../../models/User';
import { Department } from '../../models/Department';
import { environment } from '../../environments/environment';
import { ConfirmDialogComponent } from '../confirmDialog/confirmDialog';
import { UserService } from '../../service/userService';
import { UseraddComponent } from '../useradd/useradd';
import { DepartmentService } from '../../service/departmentService';
import { Loginauth } from '../../service/loginauth';
@Component({
  templateUrl: './userList.html',
  styleUrls: ['./userList.css']
})
export class UserListComponent implements OnInit {

  selection: SelectionModel<User>;
  users: User[];
  total: number;
  subscriptions: Subscription[] = [];
  pageSize = environment.pageSize;
  page: number = 1;
  departments: Department[];
  isAdmin: boolean;
  cols: string[] = [ 'name', 'brand', 'type', 'createDate', 'department', 'seatNum'];

  constructor(
    private dialog: MatDialog,
    private route: ActivatedRoute,
    private router: Router,
    private userService: UserService,
    private departmentService: DepartmentService,
    private loginauthService: Loginauth
  ) { }

  ngOnInit() {
    this.loginauthService.checkAdmin().subscribe((isAdmin: boolean) => {
      this.isAdmin = isAdmin;
      if (this.isAdmin) {
        this.cols = [ 'name', 'brand', 'type', 'createDate', 'department', 'seatNum', 'operate'];
      }
    })
    this.departmentService.getAllDepartments().subscribe((res) => {
      if(res.success) {
      this.departments = res.res;
      }
    })
    this.update(this.page).subscribe();
    const initialSelection = [];
    const allowMultiSelect = true;
    this.selection = new SelectionModel<User>(allowMultiSelect, initialSelection);
  }

  isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.users.length;
    return numSelected === numRows;
  }

  allToggle() {
    this.isAllSelected() ?
      this.selection.clear() :
      this.users.forEach(row => this.selection.select(row));
  }


  onDeleteuser(id) {
      const dialogRef = this.dialog.open(ConfirmDialogComponent, {
        width: '400px',
        height: '200px',
        data: {message: '确认删除？'}
      });
      dialogRef.afterClosed().subscribe((result) => {
        if (result) {
          this.userService.del(id).subscribe((res) => {
            if (res.success) {
              this.update(this.page).subscribe();
            }
          });
        }
      });
  }
  onopenAdd() {
      const dialogRef = this.dialog.open(UseraddComponent, {
        width: '500px',
        height: '600px',
        data: {
          departments: this.departments,
        }
      });
      dialogRef.afterClosed().subscribe((result) => {
        this.update(this.page).subscribe();
      });
  }

  onOpenEdit(user) {
      const dialogRef = this.dialog.open(UseraddComponent, {
        width: '500px',
        height: '600px',
        data: {
          user: user,
          departments: this.departments,
        }
      });
      dialogRef.afterClosed().subscribe((result) => {
        this.update(this.page).subscribe();
      });
  }

  onResetuser(userid) {
    const dialogRef = this.dialog.open(ConfirmDialogComponent, {
      width: '400px',
      height: '200px',
      data: {message: '确认重置密码么？'}
    });
    dialogRef.afterClosed().subscribe((result) => {
      if (result) {
        this.userService.resetpassword(userid).subscribe((res) => {
          if (res.success) {
            this.update(this.page).subscribe();
          }
        });
      }
    });
  }

  update(page: number = 1) {
     return this.userService.getUsers(page, this.pageSize).pipe(tap((res) => {
      if (res.success) {
        this.page = page;
        this.total = res.total;
        this.users = res.res;
      }
    }));
  }

  trackById(index: number, item: any) {
    return item.id;
  }

  onPageChanged(page: number) {
    this.page = page;
    this.update(page).subscribe();
  }

}
